export default {
	namespaced: true,
	state: {
		cart: JSON.parse(uni.getStorageSync('cart') || '[]')
	},
	mutations: {
		// 购物车添加商品
		addToCart(state, payload) {
			const res = state.cart.find(item => item.goods_id === payload.goods_id)
			if (res) {
				res.goods_count++
			} else {
				state.cart.push(payload)
			}
			uni.setStorageSync('cart', JSON.stringify(state.cart))
		},
		// 修改单个商品选中状态
		selectStatus(state, id) {
			const res = state.cart.find(item => item.goods_id === id)
			res.goods_state = !res.goods_state
			uni.setStorageSync('cart', JSON.stringify(state.cart))
		},
		// 修改商品数量
		goodsNumbeer(state, obj) {
			const res = state.cart.find(item => item.goods_id === obj.id)
			res.goods_count = obj.number
			uni.setStorageSync('cart', JSON.stringify(state.cart))
		},
		// 删除商品
		delGoods(state, obj) {
			let arr = []
			state.cart.forEach(item => {
				if (item.goods_id !== obj.goods_id) {
					arr.push(item)
				}
			})
			state.cart = arr
			uni.setStorageSync('cart', JSON.stringify(state.cart))
		},
		// 修改所有商品选择状态
		updataAllStatus(state, bool) {
			state.cart.forEach(item => item.goods_state = bool)
			uni.setStorageSync('cart', JSON.stringify(state.cart))
		}
	}
}